using System;
using CommandSystem;
using RgCore.Modules;
using Utils.NonAllocLINQ;

namespace RgCore.Commands.ModuleCommands
{
    public class Disable : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ServerConsoleCommands))
            {
                response = "你没有权限执行此命令";
                return false;
            }

            if (!ModuleManager.Plugins.TryGetFirst(x => x.Instance.Id == arguments.At(0),
                    out PluginInfo plugin))
            {
                response = "未找到此插件,可能是未安装或已禁用.";
                return false;
            }

            if (!plugin.Instance.CanUnload)
            {
                response = "无法禁用此插件，该插件被标明了无法卸载，" +
                           $"可能由于该程序集包含加载其他程序集而避免更新时程序集标识不同而出现{nameof(InvalidCastException)}问题，请使用冷重启";
                return false;
            }

            ModuleManager.DisablePlugin(arguments.At(0), true);
            response = "成功禁用此插件";
            return true;
        }

        public string Command { get; } = "disable";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "禁用一个插件";
    }
}